package com.itheima.controller;

import com.itheima.entity.Dept;
import com.itheima.entity.Result;
import com.itheima.service.DeptService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 请求处理类
 * 用于接收请求，响应数据
 */
@RequestMapping("/depts")
@RestController
public class DeptController {
    private static Logger logger = LoggerFactory.getLogger(DeptController.class);


    @Autowired//从IOC容器中，自当寻找bean对象，为该变量赋值
    private DeptService deptService;

    @GetMapping
    public Result list() {
        //1.调用servers，获取数据
        logger.info("查询全部部门数据");
        List<Dept> list = deptService.list();
        return Result.success(list);
    }

    @DeleteMapping
    public Result delete(Integer id) {
        logger.info("根据ID删除部门: {}", id);
        //调用Servers的删除方法
        deptService.delete(id);
        return Result.success("删除成功");
    }

    @PostMapping
    public Result add(@RequestBody Dept dept) {
        logger.info("新增部门: {}", dept.getName());
        deptService.add(dept);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        logger.info("根据ID查询部门: {}", id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    @PutMapping
    public Result update(@RequestBody Dept dept) {
        logger.info("更新部门信息: {}", dept.getName());
        deptService.update(dept);
        return Result.success();
    }
}